package demo5;

import java.util.*;

public class MyCircularQueue {
    //循环队列的底层就是一个数组
    public int[] arr;
    //队头
    public int front;
    //队尾
    public int tail;
    //计数
    public int count;
    public MyCircularQueue(int k) {
        arr = new int[k];
    }

    public boolean enQueue(int value) {
        if(isFull()) {
            //队列已满
            return false;
        }
        if(tail == arr.length) {
            tail = 0;
        }
        arr[tail] = value;
        tail++;
        count++;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        front++;
        if(front == arr.length) {
            front = 0;
        }
        count--;
        return true;
    }
    Queue<Integer> queue = new LinkedList<>();
    Queue<Integer> queue2 = new ArrayDeque<>();
    Deque<Integer> deque = new ArrayDeque<>();
    Deque<Integer> deque1 = new LinkedList<>();


    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return arr[front];
    }

    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        int index = (tail == 0) ? 0 : (tail - 1);
        return arr[index];
    }

    public boolean isEmpty() {
        return count == 0;
    }

    public boolean isFull() {

        return count == arr.length;
    }

    public static void main(String[] args) {
        MyCircularQueue queue = new MyCircularQueue(2);
        System.out.println(queue.enQueue(1));
        System.out.println(queue.enQueue(2));
        System.out.println(queue.deQueue());
        System.out.println(queue.enQueue(3));
        System.out.println(queue.deQueue());
        System.out.println(queue.enQueue(3));
        System.out.println(queue.deQueue());
        System.out.println(queue.enQueue(3));
        System.out.println(queue.deQueue());
        System.out.println(queue.Front());

        
    }
}
